//
//  SettingsViewController.m
//  iRSSNews
//
//  Created by bk_sport on 12/28/12.
//  Copyright (c) 2012 bk_sport. All rights reserved.
//

#import "SettingsViewController.h"
#import "ClearCacheAutoViewController.h"
#import "AppDelegate.h"

@interface SettingsViewController ()

@property (nonatomic, strong) NSArray *arrayConfig;
@property (nonatomic, strong) NSArray *arrayInfor;

@end

@implementation SettingsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"TitleSettingsKey", @"");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] init];
    self.navigationItem.backBarButtonItem.title = NSLocalizedString(@"BackKey", @"");

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    return 2;
    else return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.arrayConfig = [[NSArray alloc] init];
    self.arrayConfig = @[NSLocalizedString(@"ClearCacheKey", @""), NSLocalizedString(@"ClearCacheAutoKey", @"")];
    self.arrayInfor = [[NSArray alloc] init];
    self.arrayInfor = @[NSLocalizedString(@"ShareKey", @""), NSLocalizedString(@"AboutKey", @""), NSLocalizedString(@"HelpKey", @"")];
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // create a standardUserDefaults variable
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    // getting an NSString object
    NSString *myString = [standardUserDefaults stringForKey:@"timeToClear"];
    
    NSInteger section = [indexPath section];
    NSInteger index = [indexPath row];
    switch (section) {
        case 0:
        {
            cell.textLabel.text = [self.arrayConfig objectAtIndex:index];
            if (indexPath.row == 1) {
                cell.detailTextLabel.text = myString;
            }
            break;
        }
        case 1:
            cell.textLabel.text = [self.arrayInfor objectAtIndex:index];
            break;
    }
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return NSLocalizedString(@"ConfigKey", @"");
    else return NSLocalizedString(@"InformationKey", @"");
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger index = [indexPath row];
    switch (section) {
        case 0:
            if (index == 0) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iRSSNews" message:NSLocalizedString(@"AlertClearCacheKey", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"NoKey", @"") otherButtonTitles:NSLocalizedString(@"YesKey", @""), nil];
                [alert show];
            }
            else
            {
                ClearCacheAutoViewController *clearCache = [[ClearCacheAutoViewController alloc] init];
                clearCache.delegate = self;
                [self.navigationController pushViewController:clearCache animated:YES];
            }
            break;
        case 1: 
            if (index == 0) {
               [self actionSheet];
            }
            else if (index == 1)
            {
                AboutViewController *about = [[AboutViewController alloc] init];
                [self.navigationController pushViewController:about animated:YES];
            }
            else
            {
                HelpViewController *help = [[HelpViewController alloc] init];
                [self.navigationController pushViewController:help animated:YES];
            }
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)actionSheet{
    UIActionSheet *alert = [[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"CancelKey", @"") destructiveButtonTitle:NSLocalizedString(@"ShareMailKey", @"") otherButtonTitles:NSLocalizedString(@"ShareFacebookKey", @""),NSLocalizedString(@"ShareTwitterKey", @""), nil];
    [alert showInView:[UIApplication sharedApplication].keyWindow];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // share mail
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;
        NSArray *emailAddresses = [[NSArray alloc] initWithObjects:@"", nil];
        NSString *sendSubject = [[NSString alloc] initWithFormat:NSLocalizedString(@"TryAppKey", @"")];
        NSString *sendMessage = [[NSString alloc] initWithFormat:NSLocalizedString(@"ShareAppByMailKey", @"")];
        [mailComposer setToRecipients:emailAddresses];
        [mailComposer setSubject:sendSubject];
        [mailComposer setMessageBody:sendMessage isHTML:YES];
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    if (buttonIndex == 2) {
        TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
        NSString *link = [NSString stringWithFormat:NSLocalizedString(@"ShareAppByTwitterKey", @"")] ;
        [twitter setInitialText:link];
        [twitter addImage:[UIImage imageNamed:@"image.png"]];
        
        [self presentViewController:twitter animated:YES completion:nil];
        
        twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
            
            if(res == TWTweetComposeViewControllerResultDone) {
                
                 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SuccessKey", @"") message:NSLocalizedString(@"AlertShareTwitterOKKey", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"DismissKey", @"") otherButtonTitles: nil];
                [alert show];
                
            } 
        };
    }
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result) {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:
            break;
        case MFMailComposeResultFailed:
            break;
            
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark ClearCacheAutoViewControllerDelegate
- (void) getTimeToClear
{
    for (UITableViewCell *cell in [self.tableView visibleCells])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        [self.tableView cellForRowAtIndexPath:indexPath];

    }
        [self.tableView reloadData];
}


@end
